//
//  TGShowPhotoView.m
//  TongGuanLease
//
//  Created by 张奇 on 2022/4/23.
//

#import "TGShowPhotoView.h"
#import "TGChoosePhotoCell.h"
#import "TGPreviewFileView.h"
#import "UIButton+WebCache.h"
#import "ResetSizeImage.h"

@interface TGShowPhotoView ()<UICollectionViewDataSource, UICollectionViewDelegate, TGChoosePhotoCellDelegate>
@property (strong, nonatomic) UICollectionView *collectionView;

@end

@implementation TGShowPhotoView

- (void)awakeFromNib {
    [super awakeFromNib];
//    self.backgroundColor = UIColorTestBlue;
    [self setupUI];
}

- (instancetype)init {
    if (self = [super init] ) {
        self.backgroundColor = UIColorWhite;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
}

- (void)setImgUrlArray:(NSArray *)imgUrlArray {
    _imgUrlArray = imgUrlArray;
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDataSource, UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _imgUrlArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TGChoosePhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TGChoosePhotoCell" forIndexPath:indexPath];
    cell.row = indexPath.item;
    NSString *url = self.imgUrlArray[indexPath.row];
    
    if ([url containsString:@".pdf"]) {
//        [cell.photoBtn setImage:kImage(@"workBtnch_wlectronicFilling_pdf") forState:UIControlStateNormal];
        [cell.photoBtn setImageWithURL:[NSURL URLWithString:@"https://tgzltest.oss-cn-beijing.aliyuncs.com/upload/card/tg/workBtnch_wlectronicFilling_pdf.png"] forState:UIControlStateNormal placeholder:kImage(@"common_placehoder")];
        cell.playImageView.hidden = YES;
    } else if ([url containsString:@".xlsx"]) {
//        [cell.photoBtn setImage:kImage(@"workBtnch_wlectronicFilling_excel") forState:UIControlStateNormal];
        [cell.photoBtn setImageWithURL:[NSURL URLWithString:@"https://tgzltest.oss-cn-beijing.aliyuncs.com/upload/card/tg/workBtnch_wlectronicFilling_excel.png"] forState:UIControlStateNormal placeholder:kImage(@"common_placehoder")];
        cell.playImageView.hidden = YES;
    } else if ([url containsString:@".doc"]) {
//        [cell.photoBtn setImage:kImage(@"workBtnch_wlectronicFilling_word") forState:UIControlStateNormal];
        [cell.photoBtn setImageWithURL:[NSURL URLWithString:@"https://tgzltest.oss-cn-beijing.aliyuncs.com/upload/card/tg/workBtnch_wlectronicFilling_word.png"] forState:UIControlStateNormal placeholder:kImage(@"common_placehoder")];
        cell.playImageView.hidden = YES;
    } else if ([url containsString:@".mp4"]) {
        [TGGCD executeAsyncTask:^{
            NSString *urlStringUTF8 = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            NSURL *url = [NSURL URLWithString:urlStringUTF8];
            UIImage *image = [ResetSizeImage getVideoThumbnailWithUrl:url];
            [TGGCD executeMainAsyncTask:^{
                [cell.photoBtn setImage:image forState:UIControlStateNormal];
            }];
        }];
        
        cell.playImageView.hidden = NO;
    } else {
        NSString *urlStringUTF8 = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [cell.photoBtn setImageWithURL:[NSURL URLWithString:urlStringUTF8] forState:UIControlStateNormal placeholder:kImage(@"common_placehoder")];
        cell.playImageView.hidden = YES;
    }
    cell.delectBtn.hidden = YES;
    cell.delegate = self;
    return cell;
}

-(void)btnClick:(NSInteger)section Index:(NSInteger)row {
    NSString *url = self.imgUrlArray[row];
    if ([url containsString:@".png"] || [url containsString:@".jpg"] || [url containsString:@".mp4"]) {
        //预览图片
        NSMutableArray *photoArray = [NSMutableArray array];
        for (NSString *str in self.imgUrlArray) {
//            if ([str containsString:@".png"] || [str containsString:@".jpg"] || [str containsString:@".mp4"]) {
                [photoArray addObject:str];
//            }
        }
        NSInteger index = [photoArray indexOfObject:url];
        NSMutableArray *tem = [NSMutableArray arrayWithCapacity:photoArray.count];
        for (int i=0; i<photoArray.count; i++) {
            if ([photoArray[i] isKindOfClass:[NSString class]]) {
                NSString*urlStr = [photoArray objectAtIndex:i];
                if (![urlStr containsString:@"http"]) {
                    urlStr = StringFormat(PhotoPrefixURL, urlStr);
                }
                NSString *urlStringUTF8 = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                if ([urlStringUTF8 containsString:@".png"] || [urlStringUTF8 containsString:@".jpg"]) {
                    NSURL*url2 = [NSURL URLWithString:urlStringUTF8];
                    if (url2) {
                        [tem addObject:GetDictForPreviewPhoto(url2, ZLPreviewPhotoTypeURLImage)];
                    }
                } else if ([urlStringUTF8 containsString:@".mp4"]) {
                    NSURL*url2 = [NSURL URLWithString:urlStringUTF8];
                    if (url2) {
                        [tem addObject:GetDictForPreviewPhoto(url2, ZLPreviewPhotoTypeURLVideo)];
                    }
                }
                
            }
        }
//        if (row > tem.count-1) {return;}
        [[TGGlobalSharedInstance photoActionSheet:self.getCurrentVC] previewPhotos:tem index:index hideToolBar:YES complete:^(NSArray * _Nonnull photos) {
            NSLog(@"%@", photos);
        }];
    } else if ([url containsString:@".docx"]) {
//        [TGProgressHUD showHUDTip:@"暂不支持文件预览"];
    } else {
        TGPreviewFileView *previewView = [[TGPreviewFileView alloc] init];
        previewView.linkStr = url;
        [previewView show];
    }
  
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat width = SCREEN_WIDTH-TableViewSpace*2;
        CGFloat itemWH = (width-TableViewSpace*3.0)/4.0;
        
        flowLayout.itemSize = CGSizeMake(itemWH, itemWH);
        flowLayout.minimumLineSpacing = RatioWidth(10);
        flowLayout.minimumInteritemSpacing = RatioWidth(10);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerNib:[UINib nibWithNibName:@"TGChoosePhotoCell" bundle:nil] forCellWithReuseIdentifier:@"TGChoosePhotoCell"];
        _collectionView.backgroundColor = UIColorWhite;
        _collectionView.alwaysBounceHorizontal = NO;
        _collectionView.scrollEnabled = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
    }
    return _collectionView;
}

@end
